package com.github.gaojh.mvc.route;

import cn.hutool.core.util.ClassUtil;
import com.github.gaojh.mvc.annotation.RequestMethod;
import com.github.gaojh.server.http.HttpRequest;

/**
 * @author 高建华
 * @date 2019-05-10 11:42
 */
public enum Routers {
    /**
     * 单例
     */
    me;

    public Routers add(String path, RequestMethod requestMethod, RouteHandler routeHandler) {
        RouteInfo routeInfo = new RouteInfo();
        routeInfo.setType(routeHandler.getClass());
        routeInfo.setMethod(ClassUtil.getDeclaredMethod(routeHandler.getClass(), "handle", HttpRequest.class));
        routeInfo.setControllerObject(routeHandler);
        routeInfo.setRequestPath(path);
        routeInfo.setParams(new Object[1]);
        routeInfo.setRequestMethod(new RequestMethod[]{requestMethod});
        RouterUtil.addRouter(routeInfo);
        return this;
    }

    public Routers post(String pattern, RouteHandler routeHandler) {
        return add(pattern, RequestMethod.POST, routeHandler);
    }

    public Routers get(String pattern, RouteHandler routeHandler) {
        return add(pattern, RequestMethod.GET, routeHandler);
    }

}
